package com.memo.Adpter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.memo.Bean.recybean;
import com.memo.Interface.homeone_recy_adpter;
import com.memo.R;
import com.memo.SQL.memosql;
import com.memo.activity.EditActivity;

import java.util.ArrayList;
import java.util.List;

public class recy_adpter extends RecyclerView.Adapter<recy_adpter.ViewHolder> {
    public Activity activity;
    public List<recybean> recybeans=new ArrayList<>();
    public boolean isdeletemodel=false;
    public List<recybean> delbeans=new ArrayList<>();
    public recy_adpter(Activity activity){
        this.activity=activity;
    }

    homeone_recy_adpter homeone_recy_adpter;

    public void setHomeone_recy_adpter(com.memo.Interface.homeone_recy_adpter homeone_recy_adpter) {
        this.homeone_recy_adpter = homeone_recy_adpter;
    }

    public void Onstart(){//读取数据库数据并且刷新UI
        memosql memosql=new memosql(activity);
        Cursor cursor= memosql.getReadableDatabase().rawQuery("SELECT * FROM memo order by id desc",new String[]{});
        while (cursor.moveToNext()){
            recybean recybean=new recybean();
            recybean.id=cursor.getInt((cursor.getColumnIndex("id")));
            recybean.title=cursor.getString((cursor.getColumnIndex("title")));
            recybean.content=cursor.getString((cursor.getColumnIndex("details")));
            recybean.datatime=cursor.getString((cursor.getColumnIndex("datatime")));
            recybeans.add(recybean);
        }
        notifyDataSetChanged();
    }
    public void refresh(){//删除UI数据并重新读取数据库数据再刷新UI
        recybeans.removeAll(recybeans);
        Onstart();
    }

    public void searchstart(String SEARCHNAME){//执行查询操作并刷新UI
        recybeans.removeAll(recybeans);
        memosql memosql=new memosql(activity);
        Cursor cursor= memosql.getReadableDatabase().rawQuery("SELECT * FROM memo WHERE title LIKE '%"+SEARCHNAME+"%' or details LIKE '%"+SEARCHNAME+"%' order by id desc",new String[]{});
        while (cursor.moveToNext()){
            recybean recybean=new recybean();
            recybean.id=cursor.getInt((cursor.getColumnIndex("id")));
            recybean.title=cursor.getString((cursor.getColumnIndex("title")));
            recybean.content=cursor.getString((cursor.getColumnIndex("details")));
            recybean.datatime=cursor.getString((cursor.getColumnIndex("datatime")));
            recybeans.add(recybean);
        }
        notifyDataSetChanged();
    }

    public void delall(){
        delbeans.removeAll(delbeans);
    }

    public void deleteall(){
        memosql memosql=new memosql(activity);
        for (int i = 0; i < delbeans.size(); i++) {
            memosql.getReadableDatabase().execSQL("DELETE FROM memo Where id="+delbeans.get(i).getId()+"");
        }

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LinearLayout.inflate(activity, R.layout.item_recy_adpter,null);
        return new ViewHolder(view);
    }

    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        ViewHolder viewHolder=holder;
        viewHolder.item_recy_title.setText(recybeans.get(position).title);
        viewHolder.item_recy_content.setText(Html.fromHtml(recybeans.get(position).content));
        viewHolder.item_recy_time.setText(recybeans.get(position).datatime);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isdeletemodel){
                    Intent intent=new Intent(activity, EditActivity.class);
                    intent.putExtra("id",recybeans.get(position).id);
                    activity.startActivity(intent);
                }else {
                    if (recybeans.get(position).getIsdelmodel()){
                        viewHolder.item_recy_checkbox.setVisibility(View.GONE);
                        viewHolder.item_recy_checkbox.setChecked(false);
                        recybeans.get(position).setIsdelmodel(false);
                        delbeans.remove(recybeans.get(position));
                        homeone_recy_adpter.setdelmatch(delbeans.size());
                    }else {
                        viewHolder.item_recy_checkbox.setVisibility(View.VISIBLE);
                        viewHolder.item_recy_checkbox.setChecked(true);
                        recybeans.get(position).setIsdelmodel(true);
                        delbeans.add(recybeans.get(position));
                        homeone_recy_adpter.setdelmatch(delbeans.size());
                    }

                }

            }
        });

        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                isdeletemodel=true;
                viewHolder.item_recy_checkbox.setVisibility(View.VISIBLE);
                viewHolder.item_recy_checkbox.setChecked(true);
                recybeans.get(position).setIsdelmodel(true);
                delbeans.add(recybeans.get(position));
                homeone_recy_adpter.setdelmatch(delbeans.size());
                homeone_recy_adpter.setdelin(true);
                homeone_recy_adpter.sethometwo(false);
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {//获取list总大小
        return recybeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout item_recy_bg;
        TextView item_recy_title;
        TextView item_recy_content;
        TextView item_recy_time;
        CheckBox item_recy_checkbox;
        public ViewHolder(@NonNull View itemView) {//从view中获取标题组件,内容组件,时间组件
            super(itemView);
            item_recy_bg=itemView.findViewById(R.id.item_recy_bg);
            item_recy_title=itemView.findViewById(R.id.item_recy_title);
            item_recy_content=itemView.findViewById(R.id.item_recy_content);
            item_recy_time=itemView.findViewById(R.id.item_recy_time);
            item_recy_checkbox=itemView.findViewById(R.id.item_recy_checkbox);
        }
    }
}
